package com.example.demo.his.leetcode;

import java.util.Stack;

/**
 * 使用两个栈
 *
 * @auther zzc
 * @date 2021 03 30
 */
public class 最小栈 {

    //存元素
    private Stack<Integer> stack;
    //存最小值
    private Stack<Integer> minStack;

    public 最小栈() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    int pop() {
        if (stack.peek() == minStack.peek()) {
            minStack.pop();
        }
        return stack.pop();
    }

    int top() {
        return stack.peek();
    }

    void push(Integer num) {
        stack.push(num);
        if (!minStack.isEmpty()) {
            if (num < minStack.peek()) {
                minStack.push(num);
            }
        } else {
            minStack.push(num);
        }
    }

    int getMin() {
        return minStack.peek();
    }
}
